package xing.test.voice.jmftest;

import java.io.IOException;
import java.util.Vector;

import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.DataSink;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoDataSinkException;
import javax.media.NoProcessorException;
import javax.media.Processor;
import javax.media.format.AudioFormat;
import javax.media.protocol.DataSource;
import javax.media.protocol.FileTypeDescriptor;

import jmapps.util.StateHelper;

public class Record2File {
	public static void main(String[] args) {
		CaptureDeviceInfo di = null;
		Processor p = null;
		StateHelper sh = null;
		Vector deviceList = CaptureDeviceManager.getDeviceList(new AudioFormat(
				AudioFormat.LINEAR, 8000, 16, 2));
		if (deviceList.size() > 0)
			di = (CaptureDeviceInfo) deviceList.firstElement();
		else
			// Exit if we can't find a device that does linear,
			// 44100Hz, 16 bit,
			// stereo audio.
			System.exit(-1);
		try {
			p = Manager.createProcessor(di.getLocator());
			sh = new StateHelper(p);
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(-1);
		}
		// Configure the processor
		if (!sh.configure(10000))
			System.exit(-1);
		// Set the output content type and realize the processor
		p.setContentDescriptor(new FileTypeDescriptor(FileTypeDescriptor.WAVE));
		if (!sh.realize(10000)) {
			System.exit(-1);
		}
		// get the output of the processor
		DataSource source = p.getDataOutput();
		// create a File protocol MediaLocator with the location of the
		// file to which the data is to be written
		MediaLocator dest = new MediaLocator(
				"file://home/xing/Public/voicefoo.wav");
		// create a datasink to do the file writing & open the sink to
		// make sure we can write to it.
		DataSink filewriter = null;
		try {
			filewriter = Manager.createDataSink(source, dest);
			filewriter.open();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(-1);
		}

	}
}
